package com.hzw.saas.web.app.product.controller;


import com.hzw.saas.api.product.IPayService;
import com.hzw.saas.api.product.IUserBalanceService;
import com.hzw.saas.api.product.dto.OrderStatusDto;
import com.hzw.saas.api.product.dto.UserBalanceDto;
import com.hzw.saas.api.user.IUserAccountService;
import com.hzw.saas.api.user.bo.UserAccountBO;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.exception.DefaultSaasException;
import com.hzw.saas.common.pay.enums.PayType;
import com.hzw.saas.common.security.pojo.dto.SaasUser;
import com.hzw.saas.common.security.utils.SecurityUtils;
import com.hzw.saas.common.verify.service.IVerifyService;
import com.hzw.saas.web.app.product.dto.BalanceInfoDto;
import com.hzw.saas.web.app.product.param.BalancePayParam;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Optional;

/**
 * <p>
 * 用户余额表 前端控制器
 * </p>
 *
 * @author zzl
 * @since 2021-01-21
 */
@Controller
@RequestMapping("/user")
@RequiredArgsConstructor
@Api(tags = "会员中心/用户余额")
public class UserBalanceController {

    private final MapperFacade mapperFacade;
    private final IVerifyService verifyService;
    private final IPayService payService;
    private final IUserBalanceService userBalanceService;
    private final IUserAccountService userAccountService;

    @GetMapping("/balance")
    @SysLog
    @ApiOperation(value = "用户余额", notes = "获取用户余额信息")
    @ApiOperationSort(1)
    public ResponseEntity<BalanceInfoDto> balance() {
        SaasUser user = SecurityUtils.getUser();
        UserBalanceDto userBalanceDto = userBalanceService.get(user.getUserId());
        BalanceInfoDto balanceInfoDto = mapperFacade.map(userBalanceDto, BalanceInfoDto.class);
        return ResponseEntity.ok(balanceInfoDto);
    }

    @PostMapping("/balance/pay")
    @SysLog(operation = "余额支付", mode = 3)
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "余额支付", notes = "使用余额支付订单")
    @ApiResponses({
        @ApiResponse(code = 200, message = "请求成功"),
        @ApiResponse(code = 400, message = "请求参数异常"),
        @ApiResponse(code = 400, message = "支付错误，请重新支付")
    })
    public ResponseEntity<OrderStatusDto> balancePay(@Validated @RequestBody BalancePayParam param) {
        SaasUser user = SecurityUtils.getUser();
        Optional<UserAccountBO> account = Optional.ofNullable(userAccountService.getAccount(user.getUserId()));
        boolean verify = verifyService.verify(account.map(UserAccountBO::getPhoneNo).orElse(""), param.getVerifyCode());
        if (!verify) {
            throw DefaultSaasException.build("验证码校验失败").status(HttpStatus.BAD_REQUEST);
        }
        OrderStatusDto orderStatusDto = payService.pay(param.getPayNo(), null, PayType.BALANCE_PAY.value(), user.getUserId());
        return ResponseEntity.ok(orderStatusDto);
    }

}
